package com.ncloud.resman.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ncloud.common.utils.DateUtils;
import com.ncloud.resman.entity.ServerUnit;
import com.ncloud.resman.mapper.ServerUnitMapper;
import com.ncloud.resman.service.SequenceService;
import com.ncloud.resman.service.ServerUnitService;

@Service
public class ServerUnitServiceImpl implements ServerUnitService{
	
	@Autowired
	private ServerUnitMapper mapper;
	@Autowired
	private SequenceService sequenceService;

	@Override
	public List<ServerUnit> getUnitListByModelId(Long modelId) {
		return mapper.getUnitListByModelId(modelId);
	}

	@Override
	public void insertServerUnit(ServerUnit serverUnit) {
		Long relId = sequenceService.getSequence("rel_id_seq");
		serverUnit.setRelId(relId);
		serverUnit.setBeginDate(new Date());
		serverUnit.setEndDate(DateUtils.addYears(new Date(), 50));
		mapper.insertServerUnit(serverUnit);		
	}

	@Override
	public void updateServerUnit(ServerUnit serverUnit) {
		deleteServerUnit(serverUnit);
		insertServerUnit(serverUnit);
	}

	@Override
	public void deleteServerUnit(ServerUnit serverUnit) {
		mapper.deleteServerUnit(serverUnit);
	}

	@Override
	public void deleteServerUnitByModelId(Long modelId) {
		mapper.deleteServerUnitByModelId(modelId);
	}
}
